# -*- coding: utf-8 -*
import time
import sys
import os
import threading
from threading import Thread, current_thread
import calendar
import time
from shutil import copyfile,move
# from shutil import copyfile, copytree, move, rmtree


# time.gmtime()
class Log:
    def __init__(self):
        
        self.userName = os.popen("whoami").read()[0:-1]
        print("thread name: ",threading.currentThread().getName())
        
        self.logFolder = "/Users/{}/Library/Logs/TKProject/active/group{}".format(self.userName,threading.currentThread().getName())
        self.logFilePath = "{}/stationTestLog.log".format(self.logFolder)
        if not os.path.exists(self.logFolder):
            os.makedirs(self.logFolder)
        # self.CreateLog()

    def CreateLog(self):
        self.temp = sys.stdout
        sys.stdout = open(self.logFilePath,'a')

    def LogEnd(self):
        sys.stdout.close()
        sys.stdout = self.temp

    def LogInfo(self,*message, path=None):
        if not path:
            path = self.logFilePath
        timeNow = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        with open(path,"a") as file:
            content = "{}({})({})-->{}\n".format(timeNow,time.time(),sys.argv[0],message)
            print("111111--------",content)
            file.write(content)

    def TestFinishCreateArchiveLog(self,sn=None):
        ts = calendar.timegm(time.gmtime())
        print("ts-======",ts)
        if sn:
            self.arLogFolder = "/Users/{}/Library/Logs/TKProject/archive/{}/{}".format(self.userName,sn,ts)
        else:
            self.arLogFolder = "/Users/{}/Library/Logs/TKProject/archive/group{}/{}".format(self.userName,threading.currentThread().getName(),ts)
        if not os.path.exists(self.arLogFolder):
            os.makedirs(self.arLogFolder)

        if os.path.exists(self.logFilePath):
            print("----33333333333---",self.logFolder)
            print("----4444444---",self.arLogFolder)
            # move(self.logFolder,self.arLogFolder)
            move(self.logFilePath,self.arLogFolder)
            
        
        
if __name__ == "__main__":

    Log().LogInfo("--Station Start Test--")
    Log().TestFinishCreateArchiveLog("H31J5PDS18JP")
    # Log().TestFinishCreateArchiveLog()



